<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="..//assets/logo.png" alt />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        label-width="0px"
        :model="loginForm"
        :rules="loginFormRules"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-zhanghao"
            placeholder="请输入用户名"
            v-model="loginForm.username"
            clearable
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-mima"
            placeholder="请输入密码"
            type="password"
            v-model="loginForm.password"
            clearable
            show-password
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="success" @click="login">马上登录</el-button>
          <el-button type="warning" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        //这是登录表单的登录数据绑定
        username: "admin",
        password: "123456"
      },
      loginFormRules: {
        //验证用户名是否合法
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 5, max: 10, message: "长度在 5 到 8 个字符", trigger: "blur" }
        ],
        //验证密码是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          { min: 5, max: 10, message: "密码应是长度5~10位", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //点击重置按钮,重置登录表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          this.$message({
            showClose: true,
            message: "请检查账号和密码是否规范!",
            type: "warning"
          });
          return; //页面预验证没有通过,不发起ajax请求
        }

        //使用axios发起ajax请求,使用async中的awwit方法 {data: res}相当于res获取了data结构过来的值
        const { data: res } = await this.$http.post("login", this.loginForm);
        console.log(res);

        if (res.meta.status != 200) {
          this.$message({
            showClose: true,
            message: res.meta.msg,
            type: "error"
          });
          return;
        } else {
          //1,登录成功之后的token,保存到客户端的sessionStoreage中
          //1.1 项目中除了登录之外的其他API接口,必须在登录之后才能访问
          //1.2 token 只应在当前网站打开起见生效,所以token保存在sessionStorage 中
          sessionStorage.setItem("token", res.data.token);
          //2. 通过编程式导航跳转到后台主页,路由地址是/home
          this.$router.push("/home");
          this.$message({
            showClose: true,
            message: "登录成功",
            type: "success"
          });
        }
      });
    }
  },
  created() {
    //创建后挂载
    var that = this;
    document.onkeydown = function(event) {
      var key = event.keyCode;
      if (key == 13) {
        that.login();
      }
      if (key == 46) {
        that.resetLoginForm();
      }
    };
  }
};
</script>
<style lang="less" scoped>
.login_container {
  height: 100%;
  width: 100%;
  background-color: rgb(38, 74, 107);
  color: tomato;
  position: relative;
}
.login_box {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 500px;
  height: 300px;
  border-radius: 15px;
  background-color: #fff;
  transform: translate(-50%, -50%);
}
.avatar_box {
  position: absolute;
  left: 50%;
  z-index: 9;
  width: 110px;
  height: 110px;
  padding: 3px;
  border-radius: 50%;
  box-sizing: border-box;
  border: 1px solid #eee;
  background-color: #fff;
  box-shadow: 0 0 10px #ddd;
  transform: translate(-50%, -50%);
}
.avatar_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
.login_form {
  position: absolute;
  bottom: 25px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>